package com.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.common.BaseDAO;
import com.model.Message;
import com.page.Pagination;
import com.util.HibernateUtils;


public class MessageDAO extends BaseDAO<Message,Integer> implements IMessageDAO{

	public Pagination getAll(Pagination p){
		Session session = HibernateUtils.getSession();
		String hql = "from Message m order by m.id desc";
		Query query = session.createQuery(hql);
		query.setFirstResult((p.getPageNumber() - 1) * p.getPageSize());
		query.setMaxResults(p.getPageSize());
		List<Message> list = query.list();
		p.setData(list);
		Long count = (Long)HibernateUtils.getSession()
                .createQuery("select count(*) from Message")
                .uniqueResult();
		p.setMaxRows(count.intValue());
		session.close();
		return p;
	}
	public Pagination getAll(Pagination p,String name){
		Session session = HibernateUtils.getSession();
		String hql = "from Message m where name='"+name+"' order by m.id desc";
		Query query = session.createQuery(hql);
		query.setFirstResult((p.getPageNumber() - 1) * p.getPageSize());
		query.setMaxResults(p.getPageSize());
		List<Message> list = query.list();
		p.setData(list);
		Long count = (Long)HibernateUtils.getSession()
                .createQuery("select count(*) from Message where name='"+name+"'")
                .uniqueResult();
		p.setMaxRows(count.intValue());
		session.close();
		return p;
	}
}
